package com.mlnx.netty.handler;

/**
 * @author zzb
 * @create 2019/3/13 10:51
 */


import com.mlnx.bptp.BpPacket;
import com.mlnx.bptp.body.Body;
import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.device.manager.XmBpManager;
import com.mlnx.device.pojo.dto.AutoDetectCmdDto;
import com.mlnx.device.session.core.DeviceSession;
import com.mlnx.device.syn.SynDataManage;
import com.mlnx.netty.base.handler.BaseSimpleChannelInboundHandler;
import com.mlnx.netty.base.message.AbstractMessage;
import com.mlnx.netty.base.message.BpMessage;
import com.mlnx.netty.base.message.RegisterMessage;
import com.mlnx.netty.base.message.SynBpMessage;
import com.mlnx.tp.base.model.DeviceType;
import io.netty.channel.ChannelHandlerContext;

public class BpServerHandler extends BaseSimpleChannelInboundHandler<BpPacket> {

    private MyLog log = MyLog.getLog(BpServerHandler.class);

    @Override
    public void channelRead0(ChannelHandlerContext ctx, BpPacket bpPacket)
            throws Exception {

        Body body = bpPacket.getBody();
        AbstractMessage message = null;
        BpPacket packet = null;
        DeviceSession session = (DeviceSession) sessionManager.getSession(ctx.channel());
        if (body.getPacketType() != null) {
            switch (body.getPacketType()) {
                case ELECTRIC:
                    if (session != null) {
                        BpMessage bpMessage = new BpMessage();
                        bpMessage.setElectric(bpPacket.getBody().getElectric());
                        message = bpMessage;
                        bpMessage.setPatientId(session.getPatientId());
                        bpMessage.setDeviceId(session.getClientId());
                        log.info("收到设备电量 :" + bpPacket.getBody().getElectric());
                    }
                    break;
                case BP_PACKET:
                    if (session != null) {
                        if (deviceNettyService.getBpLastTime(session.getClientId()) == null) {

                            deviceNettyService.setBpLastTime(session.getClientId(), System.currentTimeMillis() - 200000);
                        }
                        if (deviceNettyService.getBpLastTime(session.getClientId()) >= body.getBpBody().getReceiveTime()) {
                            SynBpMessage bpMessage2 = new SynBpMessage();
                            bpMessage2.setSbp(body.getBpBody().getSbp());
                            bpMessage2.setDbp(body.getBpBody().getDbp());
                            bpMessage2.setHeart(body.getBpBody().getHr());
                            bpMessage2.setPacketTime(body.getBpBody().getReceiveTime());
                            bpMessage2.setMethod(body.getBpBody().getMethod());
                            bpMessage2.setPosition(body.getBpBody().getPosition());

                            if (body.getBpBody().getSbp() == 0 && body.getBpBody().getDbp() == 0 && body.getBpBody().getHr() == 0) {
                                SynDataManage.getInstance().verifyData(session.getClientId(), body.getBpBody().getRecord());
                                log.info("收到指定第" + (body.getBpBody().getRecord()) + "血压数据都为0 :不正常的数据");

                            } else {
                                message = bpMessage2;
                                bpMessage2.setDeviceId(session.getClientId());
//                                bpMessage2.setPatientId(session.getPatientId());
                                SynDataManage.getInstance().verifyData(session.getClientId(), body.getBpBody().getRecord());
                                log.info("收到指定第" + (body.getBpBody().getRecord()) + "条数血压结果进行处理 :" + bpMessage2.toString());
                            }

                        } else {
                            deviceNettyService.setBpLastTime(session.getClientId(), body.getBpBody().getReceiveTime());
                            BpMessage bpMessage = new BpMessage();
                            if (body.getBpBody().getSbp() == 0 && body.getBpBody().getDbp() == 0 && body.getBpBody().getHr() == 0) {
                                log.info("血压数据都为0 :不正常的数据");
                            } else {
                                bpMessage.setSbp(body.getBpBody().getSbp());
                                bpMessage.setDbp(body.getBpBody().getDbp());
                                bpMessage.setHeart(body.getBpBody().getHr());
                                bpMessage.setPacketTime(body.getBpBody().getReceiveTime());
                                bpMessage.setMethod(body.getBpBody().getMethod());
                                bpMessage.setPosition(body.getBpBody().getPosition());
                                message = bpMessage;
                                bpMessage.setDeviceId(session.getClientId());
//                                bpMessage.setPatientId(session.getPatientId());
                                log.info("收到血压结果进行处理 :" + bpMessage.toString());

                            }
                        }
                    } else {
                        BpMessage bpMessage = new BpMessage();
                        if (body.getBpBody().getSbp() == 0 && body.getBpBody().getDbp() == 0 && body.getBpBody().getHr() == 0) {
                            log.info("血压数据都为0 :不正常的数据");
                        } else {
                            BpInfo bpInfo = new BpInfo();
                            bpInfo.setSbp(body.getBpBody().getSbp());
                            bpInfo.setDbp(body.getBpBody().getDbp());
                            bpInfo.setHeart(body.getBpBody().getHr());
                            bpInfo.setPacketTime(body.getBpBody().getReceiveTime());
                            bpInfo.setMethod(body.getBpBody().getMethod());
                            bpInfo.setPosition(body.getBpBody().getPosition());

                            deviceNettyService.setXmbpCache(ctx.channel(), bpInfo);
                            log.info("收到血压结果 设备未注册，先缓存结果 :" + bpMessage.toString());

                        }
                    }
                    break;
                case REG_PACKET:
                    RegisterMessage registerMessage = new RegisterMessage();
                    message = registerMessage;
                    registerMessage.setDeviceType(DeviceType.XM_BP);
                    registerMessage.setDeviceId(body.getDeviceId());
                    registerMessage.setKeepAliveTimer(70);
                    log.debug("收到注册包进行处理");
                    break;
                case PING_PACKET:
                    packet = new BpPacket().pong();
                    ctx.channel().writeAndFlush(packet);
                    log.debug("收到ping进行处理");
                    break;
                case ASK_DEVICE_PACKET:
                    break;
                case SET_STATE_PACKET:
                    // TODO
                    // 1、获取数据组装成一个AutoDetectCmd
                    AutoDetectCmdDto autoDetectCmd1 = new AutoDetectCmdDto();
                    autoDetectCmd1.setDeviceId(session.getClientId());
                    autoDetectCmd1.setIsBegin(bpPacket.getBody().getIsBegin());
                    XmBpManager.finishSend(autoDetectCmd1);
                    log.info("回复isBegin进行处理状态为:" + autoDetectCmd1.getKey());
                    break;
                case SET_TIME_PACKET:
                    // TODO
                    // 1、获取数据组装成一个AutoDetectCmd
                    AutoDetectCmdDto autoDetectCmd2 = new AutoDetectCmdDto();
                    autoDetectCmd2.setDeviceId(session.getClientId());
                    autoDetectCmd2.setDetectTime(bpPacket.getBody().getDetectTime());
                    XmBpManager.finishSend(autoDetectCmd2);
                    log.info("回复服务器设置时间状态为:" + autoDetectCmd2.toString());
                    break;
                case START_DETECT:
                    // TODO
                    // 1、获取数据组装成一个AutoDetectCmd
                    AutoDetectCmdDto autoDetectCmd3 = new AutoDetectCmdDto();
                    autoDetectCmd3.setDeviceId(session.getClientId());
                    autoDetectCmd3.setDetectState(bpPacket.getBody().getDetectState());
                    XmBpManager.finishSend(autoDetectCmd3);
                    log.info("设备开启测量状态:" + autoDetectCmd3.toString());
                    break;
                case STOP_DETECT:
                    // TODO
                    // 1、获取数据组装成一个AutoDetectCmd
                    AutoDetectCmdDto autoDetectCmd4 = new AutoDetectCmdDto();
                    autoDetectCmd4.setDeviceId(session.getClientId());
                    autoDetectCmd4.setDetectState(bpPacket.getBody().getDetectState());
                    XmBpManager.finishSend(autoDetectCmd4);
                    log.info("设备关闭测量状态:" + autoDetectCmd4.toString());
                    break;

                case SET_RECTIFY_TIME:
                    //TODO
                    // 1、获取数据组装成一个AutoDetectCmd
                    AutoDetectCmdDto autoDetectCmd5 = new AutoDetectCmdDto();
                    autoDetectCmd5.setDeviceId(session.getClientId());
                    autoDetectCmd5.setRectify(1);
                    XmBpManager.finishSend(autoDetectCmd5);
                    log.info("回复服务器矫正时间:" + autoDetectCmd5.toString());
                    break;
                case TOTAL_NUM:
                    String total = bpPacket.getBody().getSynData();
                    SynDataManage.getInstance().setTotal(session.getClientId(), Integer.valueOf(total));
                    log.info("获得" + session.getClientId() + "设备总记录数" + total);
                    break;
                case ASK_RECROD:
                    SynBpMessage bpMessage1 = new SynBpMessage();
                    bpMessage1.setSbp(body.getBpBody().getSbp());
                    bpMessage1.setDbp(body.getBpBody().getDbp());
                    bpMessage1.setHeart(body.getBpBody().getHr());
                    bpMessage1.setPacketTime(body.getBpBody().getReceiveTime());
                    bpMessage1.setMethod(body.getBpBody().getMethod());
                    bpMessage1.setPosition(body.getBpBody().getPosition());
                    if (session != null) {
                        if (body.getBpBody().getSbp() == 0 && body.getBpBody().getDbp() == 0 && body.getBpBody().getHr() == 0) {
                            SynDataManage.getInstance().verifyData(session.getClientId(), body.getBpBody().getRecord());
                            log.info("收到指定第" + (body.getBpBody().getRecord()) + "血压数据都为0 :不正常的数据");

                        } else {
                            message = bpMessage1;
                            bpMessage1.setDeviceId(session.getClientId());
                            bpMessage1.setPatientId(session.getPatientId());
                            SynDataManage.getInstance().verifyData(session.getClientId(), body.getBpBody().getRecord());
                            log.info("收到指定第" + (body.getBpBody().getRecord()) + "条数血压结果进行处理 :" + bpMessage1.toString());
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        if (message != null) {
            ctx.fireChannelRead(message);
        }
    }


}
